﻿using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;

namespace AbpProjectTemplate.Web.Swagger
{
    /// <summary>
    /// Add enum value descriptions to Swagger
    /// </summary>
    public class SwaggerEnumFilter : ISchemaFilter
    {
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            if (context.Type.IsEnum)
            {
                schema.Enum.Clear();
                schema.Type = "string";
                schema.Format = null;
                string[] names = Enum.GetNames(context.Type);
                foreach (string text in names)
                {
                    schema.Enum.Add(new OpenApiString(text ?? ""));
                }
            }
        }
    }
}
